package com.pj.android.brower.common

import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationClientOption.AMapLocationProtocol
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode
import com.amap.api.location.AMapLocationListener
import com.pj.android.brower.business.bean.Location
import com.pj.android.brower.ui.WebActivity
import com.pj.android.relief.base.log.Logger


/**
 *
 * Created by hHui on 2020/3/11 0011.
 *
 */
class LocationManager(private val webActivity: WebActivity) : AMapLocationListener {
    private val tag = "LocationManager"

    private val locationClient = AMapLocationClient(webActivity)

    private var locationCallbackName = ""

    init {
        locationClient.setLocationOption(getDefaultOption())
        locationClient.setLocationListener(this)
    }


    override fun onLocationChanged(location: AMapLocation?) {
        if (location != null) {
            Logger.dft().i(tag, "定位成功(${location.longitude},${location.latitude})")

            val data = Location(location.longitude, location.latitude)
            webActivity.runJs(locationCallbackName, arrayOf(data.toBaiDuStr()))

        } else {
            Logger.dft().e(tag, "定位失败")
        }
    }

    fun start(locationCallbackName: String) {
        this.locationCallbackName = locationCallbackName
        locationClient.startLocation()
    }


    /**
     * 默认定位参数
     */
    private fun getDefaultOption(): AMapLocationClientOption {
        val mOption = AMapLocationClientOption()
        mOption.locationMode = AMapLocationMode.Hight_Accuracy//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.isGpsFirst = false//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.httpTimeOut = 30000//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.interval = 2000//可选，设置定位间隔。默认为2秒
        mOption.isNeedAddress = true//可选，设置是否返回逆地理地址信息。默认是true
        mOption.isOnceLocation = true//可选，设置是否单次定位。默认是false
        mOption.isOnceLocationLatest = false//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP)//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.isSensorEnable = false//可选，设置是否使用传感器。默认是false
        mOption.isWifiScan = true //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.isLocationCacheEnable = true //可选，设置是否使用缓存定位，默认为true
        mOption.geoLanguage = AMapLocationClientOption.GeoLanguage.DEFAULT//可选，设置逆地理信息的语言，默认值为默认语言（根据所在地区选择语言）
        return mOption
    }

}